package com.kbt.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.*;
import com.alipay.security.mobile.module.http.RPCUploadImpl;
import com.google.gson.Gson;
import com.kbt.adapter.GoodForOrderItemAdapter;
import com.kbt.base.BaseApplication;
import com.kbt.commont.Constants;
import com.kbt.model.data.*;
import com.kbt.model.holder.OrderInfoHolder;
import com.kbt.net.RequestManager;
import com.kbt.util.data.StaticData;
import com.kbt.util.tool.BtnSetting;
import com.kbt.util.tool.RestListViewSizeTool;
import com.kbt.util.utils.SharePreferenceUtils;
import com.kbt.util.utils.Utils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: KBT_ANDRIOD
 * @Copyright: KJ Co., Ltd. Copyright 2015-2020,  All rights reserved
 * @Description: 订单评价页面的Activity
 * @author: zhgk
 * @data: 2015/8/21 10:56
 * @version: V1.0
 */
public class OrderJudgeActivity extends Activity implements RequestManager.ResponseInterface {
    private OrderBean orderInfo;
    private OrderInfoHolder itemHolder;
    private RequestManager mRequestManager;
    private Map<String, String> map = null;
    private SharePreferenceUtils share;
    private String help;
    RatingBar good_zl;
    RatingBar good_bz;
    RatingBar kf_td;
    RatingBar kf_xy;
    RatingBar wl_td;
    RatingBar wl_xy;
    private BaseBean baseBean;
    private String state;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.order_judge);
        this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.order_judge);
        this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_return_btn);

        //设置返回按钮事件,关闭当前Activity，返回数据
        ImageView returnImg = (ImageView) findViewById(R.id.return_btn);
        returnImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //不创建地址
                setResult(RESULT_CANCELED, null);
                BaseApplication.getInstance().removeActivity(OrderJudgeActivity.this);
                finish();
            }
        });

        TextView appTitleView = (TextView) findViewById(R.id.center_title);
        appTitleView.setText("订单评价");
        //获取订单数据
        initOrderInfoData();
    }

    @Override
    protected void onStart() {
        super.onStart();
        BaseApplication.getInstance().addActivity(OrderJudgeActivity.this);
        mRequestManager = new RequestManager();
        mRequestManager.setResponseListener(this);
        map = new HashMap<String, String>();
        share = new SharePreferenceUtils(OrderJudgeActivity.this);
        //初始化订单View
        initOrderItemView();
        //初始化评价View
        initOrderJudgeView();
    }

    /***
     * 初始化评价列表
     */
    private void initOrderJudgeView() {
        //
        good_zl = (RatingBar) findViewById(R.id.good_zl);//商品质量
        good_bz = (RatingBar) findViewById(R.id.good_bz);//商品包装
        kf_td = (RatingBar) findViewById(R.id.kf_td);//客服态度
        kf_xy = (RatingBar) findViewById(R.id.kf_xy);//客服响应
        wl_td = (RatingBar) findViewById(R.id.wl_td);//物流态度
        wl_xy = (RatingBar) findViewById(R.id.wlsz);//物流速度

        final EditText proAndAdvise = (EditText) findViewById(R.id.problemAndAdvise);
        Button subBtn = (Button) findViewById(R.id.subBtn);
        subBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                help = proAndAdvise.getText().toString().trim();
                //提交评价
                httpJudge();
            }
        });


    }

    //提交评价
    private void httpJudge() {
        if(Utils.isNetworkAvailable(OrderJudgeActivity.this)){
            map.clear();
            map.put("methodType", "updatePj");
            Gson gson = new Gson();
            Map dataMap = new HashMap<String,String>();
            dataMap.put("order_no", orderInfo.getOrder_no());
            dataMap.put("order_sp_zl", good_zl.getRating());
            dataMap.put("order_sp_bz", good_bz.getRating());
            dataMap.put("order_kf_xy", kf_xy.getRating());
            dataMap.put("order_kf_td", kf_td.getRating());
            dataMap.put("order_wl_sd", wl_xy.getRating());
            dataMap.put("order_wl_td", wl_td.getRating());
            dataMap.put("judge_problem", help);
            dataMap.put("judge_user", share.getString("user_mobile", ""));
            map.put("data", gson.toJson(dataMap));
            mRequestManager.post(Constants.ServiceInterFace.judgeURL, map, Constants.RequestTag.judgeTag, BaseBean.class);
        }else{
            Toast.makeText(OrderJudgeActivity.this,R.string.Wangluo,Toast.LENGTH_SHORT).show();
        }
    }

    /******
     * 初始化订单View
     */
    private void initOrderItemView() {
        itemHolder = new OrderInfoHolder();
        itemHolder.setOrderNoView((TextView) findViewById(R.id.orderNo));
        itemHolder.setOrderCreateTimeView((TextView) findViewById(R.id.createTime));

        itemHolder.setGoodListView((ListView) findViewById(R.id.goodListForOrder));

        itemHolder.setOrderStatus((TextView) findViewById(R.id.orderStatus));

        itemHolder.setOrderCount((TextView) findViewById(R.id.orderCount));
        itemHolder.setOrderFee((TextView) findViewById(R.id.totalFeel));

        itemHolder.getOrderNoView().setText("订单号：" + orderInfo.getOrder_no());

        itemHolder.getOrderCreateTimeView().setText(orderInfo.getOrder_create_time());

        //订单商品列表
        List<OrderGoodsListBean> goodList = orderInfo.getGoods_list();
        GoodForOrderItemAdapter itemAdapter = new GoodForOrderItemAdapter(this, goodList);
        itemHolder.getGoodListView().setAdapter(itemAdapter);
        RestListViewSizeTool.setListViewHeightBasedOnChildren(itemHolder.getGoodListView());


        itemHolder.getOrderStatus().setText("待评价");

        itemHolder.getOrderCount().setText("共计 " + orderInfo.getGoods_count() + " 件");
        itemHolder.getOrderFee().setText(String.valueOf(orderInfo.getOrder_real_je()));

    }

    /*********
     * 获取订单数据
     */
    private void initOrderInfoData() {
        Intent intent = getIntent();
        state = intent.getStringExtra(StaticData.ORDER_TYPE);
        Bundle bl = intent.getBundleExtra("bundle");
        if (bl != null) {
            Object obj = bl.getSerializable("orderInfo");
            orderInfo = obj == null ? new OrderBean() : (OrderBean) obj;
        }
    }


    @Override
    public <T> void successResponse(T parsedGSON, int tag) {
        if (tag == Constants.RequestTag.judgeTag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                baseBean = (BaseBean) parsedGSON;
                if(baseBean.getResult().equals("success")){
                    Toast.makeText(OrderJudgeActivity.this,""+baseBean.getMsg(),Toast.LENGTH_SHORT).show();
                    BaseApplication.getInstance().removeActivity(OrderJudgeActivity.this);
                    finish();
                }else{
                    Toast.makeText(OrderJudgeActivity.this,""+baseBean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    @Override
    public void errorResonse(String retmeg, int tag) {
        Toast.makeText(OrderJudgeActivity.this,R.string.noWangluo,Toast.LENGTH_SHORT).show();
    }
}
